#include "apiclient.h"

apiClient::apiClient(QObject *parent)
	: QObject(parent)
{
	_active = false;
}

apiClient::~apiClient()
{
}

void								apiClient::addClient(quint16 id)
{
	_clients[id] = new clientInfo;
	QString name = QString("Speaker") + QString::number(id);
	setName(name, id);
	qDebug() << "new client added =" << name;
}

clientInfo &						apiClient::getClient(quint16 id)
{
	return *_clients[id];
}

void								apiClient::removeClient(quint16 id)
{
	delete _clients[id];
	_clients.erase(id);
	_avaibles.remove(id);
	if (_avaibles.empty()){
		_active = false;
		emit recordMod(false);
	}
}

void								apiClient::setName(QString& name, quint16 id)
{
	_clients[id]->_name = name;
	qDebug() << "the name of socket" << id << "is now" << name;
}

void								apiClient::setAvaible(bool avaible, quint16 id)
{
	_clients[id]->_avaible = avaible;
	if (avaible)
	{
		qDebug() << _clients[id]->_name << "Will now recieve data.";
		_avaibles.push_back(id);
		if (!_active){
			_active = true;
			emit recordMod(true);
		}
	}
	else
	{
		qDebug() << _clients[id]->_name << "Will not recieve data anymore.";
		_avaibles.remove(id);
		if (_avaibles.empty()){
			_active = false;
			emit recordMod(false);
		}
	}
}

void								apiClient::setConnected(bool connect, quint16 id)
{
	_clients[id]->_connected = connect;
}

std::list<quint16>					apiClient::getAvaibles() const
{
	return _avaibles;
}